package com.iris.live.services.report.extracter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Description:分析数据抽取抽象类
 * @author: Tony.Zhang
 * @Date: 2017-07-19 8:58
 **/

@Service("DataExtracter")
public class DataExtracter implements IDataExtracter {
    private static Logger logger = LoggerFactory.getLogger(DataExtracter.class);

    private Map<String, IDataExtractCommand> commands;

    public DataExtracter() {

    }

    @Override
    public void extract(Date date) throws Exception {
        this.extract(date, date);
    }

    @Override
    public void extract(String command, Date date) throws Exception {
        this.extract(command, date, date);
    }

    @Override
    public void extract(Date fromDate, Date endDate) throws Exception {
        this.extract(null, fromDate, endDate);
    }

    @Override
    public void extract(String command, Date fromDate, Date endDate) throws Exception {
        if(command == null){
            for(IDataExtractCommand extractCommand : commands.values()){
                extractCommand.execute(fromDate, endDate);
            }
        }else{
            IDataExtractCommand extractCommand = commands.get(command);
            if(extractCommand == null){
                String message = String.format("No extract command found for name=%s", command);

                logger.warn(message);
                throw new Exception(message);
            }

            extractCommand.execute(fromDate, endDate);
        }
    }
}
